<?php
/**
 * Description of ExamProcessclass
 *
 * @author Chisheng Lu, Jeffy Shih <jeffy@dorise.info>
 * @copyright Copyright(c)2010 DoRISE team, Science Education center, NTNU.
 * @since 2010-05-20
 * @version 1.1
 */
class ExamProcess {
//put your code here
    private $mysqli;

    const EPQuestion = 1;
    const EPInformation = 2;
    const EPQuestionnaire = 3;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    /**
     *
     * @param Array $aryExamProcess
     * @return Boolean
     */
    public function addExamProcess($aryExamProcess) {
        if (!array_key_exists('EBId', $aryExamProcess)) {
            throw new Exception('缺少試卷基本編號！');
        }
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_addEP = 'INSERT INTO `ExamProcess` (';
        $sql_values = ' VALUES(';

        $sql_addEP .= '`EBId` ';
        $sql_values .= $aryExamProcess['EBId'] . ' ';

        $intDoth = $this->chkProcessDoth($aryExamProcess['EBId']);

        $sql_addEP .= ', `EPDoth`';
        $sql_values .= ', ' . $intDoth;

        if (array_key_exists('EPCategory', $aryExamProcess)) {
            $sql_addEP .= ', `EPCategory`';
            $sql_values .= ', ' . $aryExamProcess['EPCategory'];
        }
        if (array_key_exists('EPQEId', $aryExamProcess)) {
            $sql_addEP .= ', `EPQEId`';
            $sql_values .= ', ' . $aryExamProcess['EPQEId'];
        }
        $sql_addEP .= ')' . $sql_values . ')';
//echo "sql_addEP:$sql_addEP<br>";
        $rs_addEP = $this->mysqli->query($sql_addEP);
        if (!$rs_addEP) {
            throw new Exception('新增試卷程序時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;

    }

    /**
     *
     * @param Integer $intEPId
     * @param Array $aryExamProcess
     * @return Boolean
     */
    public function editExamProcess($intEPId, $aryExamProcess) {
        //檢查試卷程序編號是否為空白
        if (empty ($intEPId)) {
            throw new Exception('缺少試卷程序編號！');
        }

        $bolRtn = false;
        $bolFirst = true;

        $sql_editEP = 'UPDATE `ExamProcess` SET ';
        if (array_key_exists('EBId', $aryExamProcess)) {
            $sql_editEP .= '`EBId` = ' . $aryExamProcess['EBId'];
            $bolFirst = false;
        }

        if (array_key_exists('EPDoth', $aryExamProcess)) {
            if($bolFirst) {
                $bolFirst = false;
            }else {
                $sql_editEP .= ', ';
            }
            $sql_editEP .= '`EPDoth` = ' . $aryExamProcess['EPDoth'];
        }

        if (array_key_exists('EPCategory', $aryExamProcess)) {
            if($bolFirst) {
                $bolFirst = false;
            }else {
                $sql_editEP .= ', ';
            }
            $sql_editEP .= '`EPCategory` = ' . $aryExamProcess['EPCategory'];
        }

        if (array_key_exists('EPQEId', $aryExamProcess)) {
            if($bolFirst) {
                $bolFirst = false;
            }else {
                $sql_editEP .= ', ';
            }
            $sql_editEP .= '`EPQEId` = ' . $aryExamProcess['EPQEId'];
        }

        $sql_editEP .= ' WHERE `EPId` = ' . $intEPId . ';';

        $rs_editEP = $this->mysqli->query($sql_editEP);
        if (!$rs_editEP) {
            throw new Exception('更新試卷程序時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intEPId
     * @return Boolean
     */
    public function delExamProcess($intEPId) {
        $bolRtn = false;
        //檢查試卷程序編號是否為空白
        if (empty ($intEPId)) {
            throw new Exception('錯誤！缺少試卷程序編號。');
        }

        $aryExamProcess = $this->getExamProcessInfoByEPId($intEPId);
        $intEBId = $aryExamProcess[1];
        $intEPDoth = $aryExamProcess[2];
        switch ($aryExamProcess['EPCategory']) {
            case self::EPInformation:
                include 'Information.Class.php';
                $objInfomation = new Information();
                $objInfomation->delInformation($aryExamProcess['EPQEId']);
                unset($objInfomation);
                break;
            case self::EPQuestion:
                break;
            case self::EPQuestionnaire:
                include 'Questionnaire.Class.php';
                $objQuestionnaire = new Questionnaire();
                $objQuestionnaire->delQuestionnaire($aryExamProcess['EPQEId']);
                unset ($objQuestionnaire);
                break;
        }
        //刪除指定的程序資料
        $sql_delEP = 'DELETE FROM `ExamProcess` WHERE `EPId`=' . $intEPId;
        $rs_delEP = $this->mysqli->query($sql_delEP);
        if (!$rs_delEP) {
            throw new Exception('錯誤！刪除試卷程序時發生錯誤。');
        } else {
            $sql_RecodeDoth = 'UPDATE `ExamProcess` SET `EPDoth` = `EPDoth`-1 WHERE `EBId`=' . $intEBId . ' AND `EPDoth`>' . $intEPDoth;
            $rs_RecodeDoth = $this->mysqli->query($sql_RecodeDoth);
            if(!$rs_RecodeDoth){
                throw new Exception('錯誤！重新編排試卷順序時發生錯誤。');
            }else{
                $bolRtn = true;
            }            
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intEPId ExamProcess Id
     * @return Array One dimension array
     */
    public function getExamProcessInfoByEPId($intEPId) {
        //檢查試卷程序編號是否為空白
        if (empty ($intEPId)) {
            throw new Exception('缺少試卷程序編號！');
        }

        $sql_getEPById = 'SELECT * FROM `ExamProcess` WHERE `EPId`=' . $intEPId;
        $rs_getEPById = $this->mysqli->query($sql_getEPById);

        if (!$rs_getEPById) {
            throw new Exception('無法查詢該試卷程序的資料！');
        }else {
            if ($rs_getEPById->num_rows == 0) {
                throw new Exception('無此試卷程序編號的資料！');
            }else {
                $row_getEPById = $rs_getEPById->fetch_array();
                return $row_getEPById;
            }
        } //End If

    }

    /**
     *
     * @param Integer $intEBId
     * @param String $strSelectFields
     * @return Array
     */
    public function getExamProcessInfoByEBId($intEBId, $strSelectFields = "*") {
        //檢查試卷基本資料編號是否為空白
        if (empty ($intEBId)) {
            throw new Exception('缺少試卷基本資料編號！');
        }

        $sql_getEPById = 'SELECT ' . $strSelectFields . ' FROM `ExamProcess` WHERE `EBId`=' . $intEBId . ' ORDER BY `EPDoth`';
        $rs_getEPById = $this->mysqli->query($sql_getEPById);

        if (!$rs_getEPById) {
            throw new Exception('無法查詢該試卷基本資料的資料！');
        }else {

            $aryEPList = array();
            while ($aryOneEP = $rs_getEPById->fetch_array()) {
                array_push($aryEPList, $aryOneEP);
            }
            return $aryEPList;
        } //End If

    }

    /**
     *
     * @param Integer $intEBId
     * @param Integer $intDoth
     * @return Array
     */
    private function getExamProcessInfoByEBIdAndDoth($intEBId,$intDoth) {
        $sql_getEPInfo = 'SELECT * FROM `ExamProcess` WHERE `EBId`=' . $intEBId . ' AND `EPDoth`=' . $intDoth;
        $rs_getEPInfo = $this->mysqli->query($sql_getEPInfo);

        if(!$rs_getEPInfo) {
            throw new Exception('依試卷編號及順序查詢程序資料時發生錯誤！');
        }else {
            $row_getEPInfo = $rs_getEPInfo->fetch_row();
            return $row_getEPInfo;
        }
    }

    /**
     *
     * @return Array
     */
    public function getExamProcessList() {
        $sql_getEPList = 'SELECT * FROM `ExamProcess` ;';
        $rs_getEPList = $this->mysqli->query($sql_getEPList);

        if (!$rs_getEPList) {
            throw new Exception('無法查詢該試卷程序的資料！');
        }else {
            if ($rs_getEPList->num_rows == 0) {
                throw new Exception('無此試卷程序編號的資料！');
            }else {
                $aryEPList = array();
                while ($aryOneEP = $rs_getEPList->fetch_row()) {
                    array_push($aryEPList, $aryOneEP);
                }
                return $aryEPList;
            }
        } //End If
    }

    /**
     *
     * @param Integer $intEPId
     * @return String
     */
    public function getExamProcessShortIntroByEPId($intEPId) {
        $strRtn = '';
        $row_ExamProcess = $this->getExamProcessInfoByEPId($intEPId);
        $intEPCategory = $row_ExamProcess[3];
        $intEPQEId = $row_ExamProcess[4];

        switch ($intEPCategory) {
            case self::EPQuestion:
                if (!class_exists('MainQuestion')) {
                    include 'MainQuestion.Class.php';
                }
                $objMainQuestion = new MainQuestion();
                $row_MainQuestion = $objMainQuestion->getMainQuestionInfoByMQId($intEPQEId);
                return $row_MainQuestion[1];
                break;
            case self::EPInformation:
                if(!class_exists('Information')) {
                    include 'Information.Class.php';
                }
                $objInformation = new Information();
                $row_Information = $objInformation->getInformationInfoByInfoId($intEPQEId);
                $strInfoContent = mb_substr(strip_tags($row_Information[1]), 0, 15, "UTF-8") . '...';
                return $strInfoContent;
                break;
            case self::EPQuestionnaire:
                if (!class_exists('QnContent')) {
                    include 'QnContent.Class.php';
                }
                $objQnContent = new QnContent();
                $aryQnItemTitle = $objQnContent->getQnContentItemCTitleByQnId($intEPQEId);
                $strQnContent = mb_substr(implode($aryQnItemTitle, ', '), 0, 30, "UTF-8") . '...';
                return $strQnContent;
                break;
        }

    }

    /**
     *
     * @param Integer $intEBId
     * @param Integer $intEPCategory
     * @param Integer $intEPQEId
     * @return Boolean
     */
    public function chkProcessExist($intEBId,$intEPCategory,$intEPQEId) {
        $bolRtn = true;
        if(empty($intEBId) || empty($intEPCategory) || empty($intEPQEId)) {
            throw new Exception('缺少試卷編號或試卷類別或個別編號！');
        }

        $sql_chkExist = 'SELECT * FROM `ExamProcess` WHERE `EBId`=' . $intEBId . '
            AND `EPCategory`=' . $intEPCategory . '
            AND `EPQEId`=' . $intEPQEId;
        $rs_chkExist = $this->mysqli->query($sql_chkExist);

        if(!$rs_chkExist) {
            throw new Exception('確認試題是否存在時發生問題！');
        }else {
            if($rs_chkExist->num_rows == 0) {
                $bolRtn = false;
            }
        }

        return $bolRtn;
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    /**
     *
     * @param Integer $intEBId
     * @return Integer
     */
    private function chkProcessDoth($intEBId) {
        $intDoth = 0;

        if (empty ($intEBId)) {
            throw new Exception('缺少試卷基本編號！');
        }

        $sql_selEP = "SELECT MAX(`EPDoth`) FROM `ExamProcess` WHERE `EBID` = " . $intEBId;
        $rs_selEP = $this->mysqli->query($sql_selEP);

        if (!$rs_selEP) {
            throw new Exception('新增試卷程序時，決定順序，發生錯誤！');
        }else {
            $row_Doth = $rs_selEP->fetch_row();
            if (is_null($row_Doth[0])) {
                $intDoth = 1;
            } else {
                $intDoth = $row_Doth[0] + 1;
            }
        }

        return $intDoth;
    }

    /**
     *
     * @param String $strMoveDirect
     * @param Integer $intEPId
     * @return Boolean
     */
    public function moveExamProcess($strMoveDirect,$intEPId) {
        $bolRtn = false;
        if(empty($strMoveDirect) || empty($intEPId)) {
            throw new Exception('缺少試卷程序編號！');
        }

        $row_EPInfo = $this->getExamProcessInfoByEPId($intEPId);
        $intEBId = $row_EPInfo[1];
        $intThisDoth = $row_EPInfo[2];

        if($strMoveDirect == 'down') {
            $row_EPNext = $this->getExamProcessInfoByEBIdAndDoth($intEBId, $intThisDoth+1);
            $intNextEPId = $row_EPNext[0];

            //目前的順序+1存入
            $aryThisExamProcess = array('EPDoth'=>$intThisDoth + 1);

            if($this->editExamProcess($intEPId, $aryThisExamProcess)) {
                //下一筆順序耕改為目前的順序
                $aryNextExamProcess = array('EPDoth'=>$intThisDoth);
                if($this->editExamProcess($intNextEPId, $aryNextExamProcess)) {
                    $bolRtn = true;
                }
            }

        }elseif($strMoveDirect == 'up') {
            $row_EPNext = $this->getExamProcessInfoByEBIdAndDoth($intEBId, $intThisDoth-1);
            $intNextEPId = $row_EPNext[0];

            //目前的順序-1存入
            $aryThisExamProcess = array('EPDoth'=>$intThisDoth - 1);

            if($this->editExamProcess($intEPId, $aryThisExamProcess)) {
                //下一筆順序耕改為目前的順序
                $aryNextExamProcess = array('EPDoth'=>$intThisDoth);
                if($this->editExamProcess($intNextEPId, $aryNextExamProcess)) {
                    $bolRtn = true;
                }
            }
        }

        return $bolRtn;
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
